package com.ios.service.impl;

import com.ios.dao.DisablerUserMapper;
import com.ios.dao.MessageMapper;
import com.ios.dto.MessageExecution;
import com.ios.entity.Message;
import com.ios.enums.MessageStateEnum;
import com.ios.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MessageServiceImpl implements IMessageService{

    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private DisablerUserMapper disablerUserMapper;

    @Override
    public MessageExecution PublishMessage(Integer disabler_id, String context) {
        if (String.valueOf(disabler_id).equals(null)|| context.equals(null)){
            /*检查传入参数是否为空*/
            return new MessageExecution(MessageStateEnum.NULL_PARAM);
        }else if (!CheckDisablerId(disabler_id)){
            /*检查用户是否存在*/
            return new MessageExecution(MessageStateEnum.NULL_DISABLER);
        }else {
            try {
                /*发布文章操作*/
                Message message = new Message();
            /*设置状态值：审核中*/
                message.setStatue(0);
            /*设置时间*/
                message.setCreateTime(new Date());
                message.setUpdateTime(new Date());
            /*设置其他参数*/
                message.setDisablerId(disabler_id);
                message.setContent(context);
                messageMapper.insert(message);
                return new MessageExecution(MessageStateEnum.REVIEW_ING,message);
            }catch (Exception e){
                return new MessageExecution(MessageStateEnum.FAIL_PUBLISH);
            }
        }
    }

    @Override
    public MessageExecution DeleteMessage(Integer id) {
        if (String.valueOf(id).equals(null)) {
            /*检查传入参数是否为空*/
            return new MessageExecution(MessageStateEnum.NULL_PARAM);
        } else if (!CheckId(id)) {
            /*检查id是否存在*/
            return new MessageExecution(MessageStateEnum.NULL);
        } else {
            try {
                messageMapper.deleteByPrimaryKey(id);
                return new MessageExecution(MessageStateEnum.SUCCESS_DELECTE);
            } catch (Exception e) {
                return new MessageExecution(MessageStateEnum.FAIL_DELETE);
            }
        }
    }

    @Override
    public MessageExecution ShowAllMessage(){
        /*检查文章状态，只展示审核通过的，即statue=1*/
        try {
            List<Message> messageList = messageMapper.SelectAll();
            List<Message> showList = new ArrayList<Message>();
            for(Message message:messageList){
                if (message.getStatue()==1){
                    showList.add(message);
                }
            }
            return new MessageExecution(MessageStateEnum.SUCCESS_SHOW,showList);
        }catch (Exception e){
            return new MessageExecution(MessageStateEnum.FAIL_SHOW);
        }
    }

    @Override
    public MessageExecution ShowMessageByDisablerId(Integer disablerid) {
        /*检查文章状态，只展示审核通过的，即statue=1*/
        if (!CheckDisablerId(disablerid)){
            return new MessageExecution(MessageStateEnum.NULL_DISABLER);
        }else {
            try {
                List<Message> messageList = messageMapper.SelectByDisablerId(disablerid);
                List<Message> showlist = new ArrayList<Message>();
                for(Message message:messageList){
                    if (message.getStatue()==1){
                        showlist.add(message);
                    }
                }
                return new MessageExecution(MessageStateEnum.SUCCESS_SHOW,showlist);
            }catch (Exception e){
                return new MessageExecution(MessageStateEnum.FAIL_SHOW);
            }
        }
    }

    /*判断用户是否存在*/
    private boolean CheckDisablerId(Integer disablerId){
        List<Integer > AllId = disablerUserMapper.getAllDisablerId();
        for (Integer id : AllId){
            if (id.equals(disablerId)){
                return true;
            }
        }
        return false;
    }

    /*判断文章是否存在*/
    private boolean CheckId(Integer id){
        List<Integer> list = messageMapper.ShowAllId();
        for (Integer id_1:list){
            if (id==id_1){
                return true;
            }
        }
        return false;
    }
}
